package com.zerdoor.service.impl;

import com.zerdoor.dao.IForgetPassWordDao;
import com.zerdoor.model.UserInfo;
import com.zerdoor.service.IForgetPassWordService;
import com.zerdoor.util.Email;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zjy on 2015/5/7.
 */
@Service
public class ForgetPassWordServiceImpl implements IForgetPassWordService {

    @Autowired
    private IForgetPassWordDao iForgetPassWordDao;


    @Override
    public Map<String, String> updatePwdByUserInfo(String name, String email, String phone,Email simpleMail,String fromMail) throws Exception {

        Map<String,String> msgMap = new HashMap<String,String>();

        UserInfo userInfo = iForgetPassWordDao.findUserInfoByName(name);

        msgMap.put("status","false");//状态

        if(userInfo == null){

            msgMap.put("errorLabel","userImg");
            msgMap.put("msg","该账号不存在！");

            return msgMap;
        }
        if(!userInfo.getEmail().equalsIgnoreCase(email)){

            msgMap.put("errorLabel","emailImg");
            msgMap.put("msg","认证邮箱不匹配！");

            return msgMap;
        }
        if(!userInfo.getPhone().equalsIgnoreCase(phone)){

            msgMap.put("errorLabel","phoneImg");
            msgMap.put("msg","注册手机号不匹配！");

            return msgMap;
        }
        if(userInfo.getEmailAuth() == null){
            userInfo.setEmailAuth(101002);
        }
        if(userInfo.getEmailAuth() != 101001){

            msgMap.put("errorLabel","emailImg");
            msgMap.put("msg","邮箱未认证，请联系管理员重置密码！");

            return msgMap;
        }

        iForgetPassWordDao.updateUserPwdByUserId(userInfo.getUserId());

        SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");//设置日期格式
        String date = df.format(new Date());// new Date()为获取当前系统时间

        //发邮件
        simpleMail.sendMail("ACG平台密码重置", "您于 " + date + " 成功申请了密码重置，重置密码为 123456 请前往ACG平台修改密码！http://www.waibao51.com/",fromMail,email);

        msgMap.put("status", "true");//状态
        msgMap.put("msg","密码重置成功，新密码已发往您的认证邮箱！");
        return msgMap;
    }
}
